import { YYYY_MM_DD, YYYY_MM_DD_HH_MM_SS } from '../utils/utils';
import { Menu } from './../entitys/Menu';
import { Role } from './../entitys/Role';
import { getRepository, In, FindConditions, Like } from 'typeorm';
import { Inject, Service } from "typedi";
import * as moment from "moment"
import Page from '../common/page';
import MenuService from './MenuService';
@Service()
export default class RoleService {

    @Inject()
    menuService: MenuService
    /**
     * 添加角色
     * @param roleName 
     * @param roleDesc 
     * @param menuIds 
     */
    async addRole(roleName: string, roleDesc: string, menuIds: string[]) {
        const roleRepository = getRepository(Role)
        let role = new Role()
        role.roleName = roleName
        role.roleDesc = roleDesc
        role.createTime = new Date()
        role.updateTime = new Date()
        role.dataStatus = 1
        role.menus = await this.menuService.getMenusByIds(menuIds)
        await roleRepository.save(role)
        console.log(role)
        return role
    }

    async getRoleById(roleId: number) {
        const roleRepository = getRepository(Role)
        const role = await roleRepository.findOne(roleId, {
            relations: ["menus"]
        })
        return role
    }
    async delRole(id: number) {
        const roleRepository = getRepository(Role)
        const role = await this.getRoleById(id)
        if (!role) {
            throw new Error("未找到指定id的角色")
        }
        await roleRepository.delete(role.id)
    }
    /**
     * 通过roleName获取角色
     * @param roleName 
     */
    async selectRoleByRoleName(roleName: string) {
        const roleRepository = getRepository(Role)
        const role = await roleRepository.findOne({ roleName })
        return role
    }
    /**
     * 更新角色
     * @param roleId 
     * @param roleName 
     * @param roleDesc 
     * @param menuIds 
     */
    async updateRole(roleId: number, roleName: string, roleDesc: string, menuIds: string[], dataStatus: number) {
        const roleRepository = getRepository(Role)
        let role = await roleRepository.findOne(roleId)
        roleName && (role.roleName = roleName)
        roleDesc && (role.roleDesc = roleDesc)
        if (!(dataStatus < 0 || dataStatus > 1)) {
            role.dataStatus = dataStatus
        }
        role.menus = await this.menuService.getMenusByIds(menuIds)
        await roleRepository.save(role)
        return true
    }
    async getRoleByConds(roleName: string,
        roleDesc: string,
        dataStatus: number,
        createTimeRange: string[],
        updateTimeRange: string[],
        page: number,
        pageSize: number) {
        const reoleRepository = getRepository(Role)
        let queryBuilder = reoleRepository.createQueryBuilder("role")
        queryBuilder.where(" 1=1 ")
        if (roleName) {
            queryBuilder.andWhere("role.roleName like :roleName", { roleName: "%" + roleName + "%" })
        }
        if (roleDesc) {
            queryBuilder.andWhere("role.roleDesc like :roleDesc", { roleDesc: "%" + roleDesc + "%" })
        }
        if ((dataStatus || dataStatus === 0) && !(dataStatus < 0 || dataStatus > 1)) {
            queryBuilder.andWhere("role.dataStatus = :dataStatus", { dataStatus })
        }
        if (createTimeRange) {
            queryBuilder.andWhere(" role.createTime between :createTimeStart and :createTimeEnd",
                {
                    createTimeStart: moment(createTimeRange[0], YYYY_MM_DD).toDate(),
                    createTimeEnd: moment(createTimeRange[1], YYYY_MM_DD).toDate()
                })
        }
        if (updateTimeRange) {
            queryBuilder.andWhere(" role.updateTime between :updateTimeStart and :updateTimeEnd",
                {
                    updateTimeStart: moment(updateTimeRange[0], YYYY_MM_DD).toDate(),
                    updateTimeEnd: moment(updateTimeRange[1], YYYY_MM_DD).toDate()
                })
        }
        let count = await queryBuilder.getCount()
        const pageObj = new Page(count, page, pageSize)
        queryBuilder.limit(pageObj.pageSize).offset(pageObj.startIndex)
        let roles = await queryBuilder.getMany()
        roles.forEach((role: any) => {
            role.createTime = moment(role.createTime).format(YYYY_MM_DD_HH_MM_SS)
            role.updateTime = moment(role.updateTime).format(YYYY_MM_DD_HH_MM_SS)
        })
        pageObj.list = roles;
        return pageObj
    }
    async getAllRole() {
        return await getRepository(Role).find({ dataStatus: 1 })
    }
}